package com.limanyik.family.oss.service.impl;

import com.limanyik.family.common.entity.BasisAlibabaOssFile;
import com.limanyik.family.common.entity.server.oss.dto.DownloadTheFileDto;
import com.limanyik.family.common.entity.server.oss.dto.UploadFileDto;
import com.limanyik.family.common.enums.oss.OssFileEnumInterface;
import com.limanyik.family.common.utils.FamilyHttpResponse;
import com.limanyik.family.oss.config.OssAccessConfig;
import com.limanyik.family.oss.config.OssUploadingServiceConfiguration;
import com.limanyik.family.oss.service.BaseUploadService;
import com.limanyik.family.oss.service.BasisAlibabaOssFileService;
import lombok.SneakyThrows;
import lombok.extern.java.Log;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@Service
@Log
public class BaseUploadServiceImpl implements BaseUploadService {

    @Resource(name = "ossAccessConfig")
    OssAccessConfig ossAccessConfig;


    @Resource(name = "ossUploadingServiceConfiguration")
    OssUploadingServiceConfiguration ossUploadingServiceConfiguration;

    @Resource(name = "basisAlibabaOssFileServiceImpl")
    BasisAlibabaOssFileService basisAlibabaOssFileService;

    private String WEB_HEAD = "https://";

    @SneakyThrows
    @Override
    public FamilyHttpResponse uploadAliyunChannel(UploadFileDto uploadFileDto, MultipartFile file) {
        // 上传文件成功
        ossUploadingServiceConfiguration.uploadFiles(uploadFileDto, file);
        // 添加数据库记录
        BasisAlibabaOssFile basisAlibabaOssFile = new BasisAlibabaOssFile();
        String suffix = file.getOriginalFilename();
        suffix = suffix.substring(suffix.lastIndexOf(".") + 1);
        basisAlibabaOssFile.setBucketId("d3f03ebc50ed11ec9384305a3ae09624");
        // 拼接网络文件下载地址：https://jakers.oss-cn-beijing.aliyuncs.com
        basisAlibabaOssFile.setPublicNetworkDownloadUrl(WEB_HEAD + ossAccessConfig.getHTTPS_DOWNLOAD_URL() + uploadFileDto.getUrl() + uploadFileDto.getKey());
        basisAlibabaOssFile.setName(uploadFileDto.getKey());
        basisAlibabaOssFile.setSize(file.getSize());
        basisAlibabaOssFile.setSuffix(suffix);
        basisAlibabaOssFile.setUploadTheUrl(uploadFileDto.getUrl());
        basisAlibabaOssFile.setWhetherTheServerIsEncrypted(OssFileEnumInterface.OssServerEncryptEnum.NOT_ENCRYPT.getCode());
        basisAlibabaOssFile.setServerSideEncryption("");
        basisAlibabaOssFile.setCreatorId(uploadFileDto.getBasisUser().getId());
        basisAlibabaOssFile.setCreatorName(uploadFileDto.getBasisUser().getNickName());
        basisAlibabaOssFile.setUpdaterId(uploadFileDto.getBasisUser().getId());
        basisAlibabaOssFile.setUpdaterName(uploadFileDto.getBasisUser().getNickName());
        basisAlibabaOssFile.setServerNetworkDownloadUrl("");
        basisAlibabaOssFile.setTypeCode("");
        basisAlibabaOssFile.setAcl("");
        basisAlibabaOssFile.setStorageClass("");
        return basisAlibabaOssFileService.save(basisAlibabaOssFile) ? FamilyHttpResponse.success() : FamilyHttpResponse.error("文件上传失败！！");
    }

    @Override
    public FamilyHttpResponse downloadAliyunChannel(HttpServletResponse response, DownloadTheFileDto downloadTheFileDto) {
        ossUploadingServiceConfiguration.downloadTheFile(response, downloadTheFileDto);
        return FamilyHttpResponse.success();
    }
}
